﻿using Coldairarrow.Entity.Base;
using Coldairarrow.Entity.Base_Manage;
using Coldairarrow.IBusiness;
using Coldairarrow.Util;
using EFCore.Sharding;
using LinqKit;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Linq.Expressions;
using System.Threading.Tasks;

namespace Coldairarrow.Business.Base
{
    public partial class Base_RegistrationBusiness : BaseBusiness<Base_Registration>, IBase_RegistrationBusiness, ITransientDependency
    {
        readonly IOperator _operator;
        public Base_RegistrationBusiness(IDbAccessor db, IOperator @operator)
            : base(db)
        {
            _operator = @operator;
        }

        #region 外部接口

        public async Task<PageResult<Base_Registration>> GetDataListAsync(PageInput<ConditionDTO> input)
        {
            var q = GetIQueryable();
            var where = LinqHelper.True<Base_Registration>();
            var search = input.Search;

            //筛选
            if (!search.Condition.IsNullOrEmpty() && !search.Keyword.IsNullOrEmpty())
            {
                var newWhere = DynamicExpressionParser.ParseLambda<Base_Registration, bool>(
                    ParsingConfig.Default, false, $@"{search.Condition}.Contains(@0)", search.Keyword);
                where = where.And(newWhere);
            }

            return await q.Where(where).GetPageResultAsync(input);
        }

        public async Task<Base_Registration> GetTheDataAsync(string id)
        {
            return await GetEntityAsync(id);
        }

        [DataAddLog(UserLogType.看板注册, "Name", "看板")]
        [DataRepeatValidate(new string[] { "Code" }, new string[] { "编号" })]
        public async Task AddDataAsync(Base_Registration data)
        {
            data.QrCode = Registration + "," + data.Id;
            await InsertAsync(data);
        }

        [DataEditLog(UserLogType.看板注册, "Name", "看板")]
        [DataRepeatValidate(new string[] { "Code" }, new string[] { "编号" })]
        public async Task UpdateDataAsync(Base_Registration data)
        {
            data.QrCode = Registration + "," + data.Id;
            await UpdateAsync(data);
        }

        [DataDeleteLog(UserLogType.看板注册, "Name", "看板")]
        public async Task DeleteDataAsync(List<string> ids)
        {
            await DeleteAsync(ids);
        }

        /// <summary>
        /// 获取看板注册列表
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<PageResult<Base_RegistrationDto>> GetBase_RegistrationList(PageInput<Base_RegistrationCondition> input)
        {
            var search = input.Search;
            Expression<Func<Base_Registration, Base_Department, Base_Department, Base_RegistrationDto>> select = (a, b, c) => new Base_RegistrationDto
            {
                DepartmentName = b.Name,
                DeptName = c.Name
            };
            select = select.BuildExtendSelectExpre();
            var q = from a in GetIQueryable().AsExpandable()
                    join b in Db.GetIQueryable<Base_Department>() on a.DepartmentId equals b.Id into ab
                    from b in ab.DefaultIfEmpty()
                    join c in Db.GetIQueryable<Base_Department>() on a.DeptId equals c.Id into ac
                    from c in ac.DefaultIfEmpty()
                    select @select.Invoke(a, b, c);
            if (!input.Search.Keword.IsNullOrEmpty())
            {
                q = q.Where(x => x.Code.Contains(input.Search.Keword) || x.Name.Contains(input.Search.Keword));
            }
            if (!input.Search.DepartmentName.IsNullOrEmpty())
            {
                q = q.Where(x => x.DepartmentName.Contains(input.Search.DepartmentName));
            }
            q = q.OrderByDescending(m => m.CreateTime);
            q = q.Where(m=>_operator.Property.DeptIds.Contains(m.DepartmentId));
            return await q.GetPageResultAsync(input);
        }

        public async Task<Base_RegistrationDtoDeptList> GetRegistrationDtoDeptList(string DeviceId)
        {
            Expression<Func<Base_Registration, Base_Department, Base_Department, Base_RegistrationDtoDeptList>> select = (a, b, c) => new Base_RegistrationDtoDeptList
            {
                Id= a.Id,
                Code = a.Code,
                DeptId = b.Id,
                DepartmentId = c.Id,
                DepartmentName = b.Name + "-" + c.Name,
            };
            select = select.BuildExtendSelectExpre();
            var q = from a in Db.GetIQueryable<Base_Registration>().AsExpandable()
                    join b in Db.GetIQueryable<Base_Department>() on a.DeptId equals b.Id into ab
                    from b in ab.DefaultIfEmpty()
                    join c in Db.GetIQueryable<Base_Department>() on a.DepartmentId equals c.Id into ac
                    from c in ac.DefaultIfEmpty()
                    select @select.Invoke(a, b, c);

            return await q.FirstOrDefaultAsync(s=>s.Code.Equals(DeviceId));


        }

        #endregion

        #region 私有成员

        #endregion
    }
}